home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / IFLINK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-29  |  2.4 KB  |  144 lines

  1. /*
  2.  * MiNT-Net specific functions for linking tty devices to network inter-
  3.  * faces.
  4.  * (w) '94, Kay Roemer <roemer@rbi.informatik.uni-frankfurt.de>.
  5.  */
  6.  
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <netinet/in.h>
  14. #include <net/if.h>
  15. #include "iflink.h"
  16.  
  17. /*
  18.  * link device to network interface.
  19.  */
  20. int
  21. if_link (device, ifname)
  22.     char *device, *ifname;
  23. {
  24.     extern int _unx2dos (const char *, char *);
  25.     struct iflink ifl;
  26.     int sockfd;
  27.     long r;
  28.  
  29.     sockfd = socket (PF_INET, SOCK_DGRAM, 0);
  30.     if (sockfd < 0) {
  31.         return -1;
  32.     }
  33.     _unx2dos (device, ifl.device);
  34.     strncpy (ifl.ifname, ifname, sizeof (ifl.ifname));
  35.     r = ioctl (sockfd, SIOCSIFLINK, &ifl);
  36.     if (r < 0) {
  37.         close (sockfd);
  38.         return -1;
  39.     }
  40.     close (sockfd);
  41.     strncpy (ifname, ifl.ifname, sizeof (ifl.ifname));
  42.     return 0;
  43. }
  44.  
  45. /*
  46.  * Get interface link level flags
  47.  */
  48. int
  49. if_getlnkflags (ifname, flags)
  50.     char *ifname;
  51.     short *flags;
  52. {
  53.     struct ifreq ifr;
  54.     int sock;
  55.  
  56.     sock = socket (PF_INET, SOCK_DGRAM, 0);
  57.     if (sock < 0) {
  58.         return -1;
  59.     }
  60.     strcpy (ifr.ifr_name, ifname);
  61.     if (ioctl (sock, SIOCGLNKFLAGS, &ifr) < 0) {
  62.         close (sock);
  63.         return -1;
  64.     }
  65.     *flags = ifr.ifr_flags;
  66.     close (sock);
  67.     return 0;
  68. }
  69.  
  70. /*
  71.  * Set interface link level flags
  72.  */
  73. int
  74. if_setlnkflags (ifname, flags)
  75.     char *ifname;
  76.     short flags;
  77. {
  78.     struct ifreq ifr;
  79.     int sock;
  80.  
  81.     sock = socket (PF_INET, SOCK_DGRAM, 0);
  82.     if (sock < 0) {
  83.         return -1;
  84.     }
  85.     strcpy (ifr.ifr_name, ifname);
  86.     ifr.ifr_flags = flags;
  87.     if (ioctl (sock, SIOCSLNKFLAGS, &ifr) < 0) {
  88.         close (sock);
  89.         return -1;
  90.     }
  91.     close (sock);
  92.     return 0;
  93. }
  94.  
  95. /*
  96.  * Get interface flags
  97.  */
  98. int
  99. if_getifflags (ifname, flags)
  100.     char *ifname;
  101.     short *flags;
  102. {
  103.     struct ifreq ifr;
  104.     int sock;
  105.  
  106.     sock = socket (PF_INET, SOCK_DGRAM, 0);
  107.     if (sock < 0) {
  108.         return -1;
  109.     }
  110.     strcpy (ifr.ifr_name, ifname);
  111.     if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) {
  112.         close (sock);
  113.         return -1;
  114.     }
  115.     *flags = ifr.ifr_flags;
  116.     close (sock);
  117.     return 0;
  118. }
  119.  
  120. /*
  121.  * Set interface link level flags
  122.  */
  123. int
  124. if_setifflags (ifname, flags)
  125.     char *ifname;
  126.     short flags;
  127. {
  128.     struct ifreq ifr;
  129.     int sock;
  130.  
  131.     sock = socket (PF_INET, SOCK_DGRAM, 0);
  132.     if (sock < 0) {
  133.         return -1;
  134.     }
  135.     strcpy (ifr.ifr_name, ifname);
  136.     ifr.ifr_flags = flags;
  137.     if (ioctl (sock, SIOCSIFFLAGS, &ifr) < 0) {
  138.         close (sock);
  139.         return -1;
  140.     }
  141.     close (sock);
  142.     return 0;
  143. }
  144.